class @AdminBookEditView extends CustomView

    el: $('#content')

    initialize: ->
        super
        @model.bind("change", @render)
        @model.fetch()

    render: ->
        model = @model
        el = $(@el)

        @GetTemplate(SiteMap.Admin.Books.EditTemplate, (compiledTemplate) ->
            el.html(compiledTemplate(model.toJSON()))

            $("#Authors").combobox({
                isMulti: true
                isDynamic: true
                source: SiteMap.Admin.Shared.GetAuthors
                valueSource: SiteMap.Admin.Shared.GetAuthorsForBook+"?bookId=#{model.get('BookId')}"
            })
            $("#Categories").combobox({
                isMulti: true
                values: model.get("Categories")
            })
            $("#DateOfPublish").datepicker()
            $("#Avatar").ImageUpload()
            $("#UserId").combobox({
                    source: SiteMap.Admin.Books.GetUsers + "?bookId=" + model.get('BookId')
            });
            $("#FileName").FileUploader()
        )

    events:
        "click .buttons>button.save": "save"
        "click .buttons>button.cancel": "cancel"

    save: (e) ->
        e.preventDefault()
        Loading.Start()
        data = $(@el).find("form.general").serialize()
        Messages.WriteMessage('Saving book.')

        $.ajax(
            type: 'POST'
            url: SiteMap.Admin.Books.Edit + "?bookId=" + @model.get("BookId")
            data: data
            success: (response) =>
                if response.SuccessMessage? && response.SuccessMessage.length
                    Messages.WriteSuccessMessage(response.SuccessMessage)
                    @Navigate(SiteMap.Admin.Books.Listing, true)
                else
                    Messages.WriteErrors(response.Errors)
                    Loading.Stop()
        )

    cancel: (e) ->
        e.preventDefault()
        Loading.Stop()
        Messages.Clear()
        @Navigate(SiteMap.Admin.Books.Listing, true)